package com.shanzmoo.job.task;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.shanzmoo.core.util.RedisCoreKeyUtils;
import com.shanzmoo.job.base.BaseTask;
import com.shanzmoo.base.constant.MallOrderStatus;
import com.shanzmoo.db.mall.entity.MallOrderDetailEntity;
import com.shanzmoo.db.mall.entity.MallOrderEntity;
import com.shanzmoo.db.mall.service.IMallOrderDetailService;
import com.shanzmoo.db.mall.service.IMallOrderService;
import com.shanzmoo.db.order.entity.dto.MallOrderDeatilRedisDTO;
import com.shanzmoo.db.order.entity.dto.MallOrderRedisDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 商城订单全量缓存刷新 定时任务
 *
 * @author： Gzhao 2021/1/17
 */
@Slf4j
@Component(value = "mallOrderAllRedisRefreshTask")
public class MallOrderAllRedisRefreshTask implements BaseTask {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private IMallOrderService mallOrderService;

    @Autowired
    private IMallOrderDetailService mallOrderDetailService;

    @Override
    public String doJob() {
        List<MallOrderEntity> allOrderList = mallOrderService.list(
                new QueryWrapper<MallOrderEntity>()
                        .eq("deleted", false)
                        .ne("status", MallOrderStatus.CLOSED)
                        .orderByDesc("update_time")
        );

        if (CollectionUtil.isNotEmpty(allOrderList)) {
            // 基础订单数据
            stringRedisTemplate.delete(RedisCoreKeyUtils.installAllOrderListKey());
            for (MallOrderEntity mallOrderEntity : allOrderList) {
                MallOrderRedisDTO redisDTO = JSON.parseObject(JSON.toJSONString(mallOrderEntity), MallOrderRedisDTO.class);

                stringRedisTemplate.opsForHash().put(RedisCoreKeyUtils.installAllOrderListKey(), String.valueOf(mallOrderEntity.getId()), JSON.toJSONString(redisDTO));
            }

            // 用户订单
            stringRedisTemplate.delete(RedisCoreKeyUtils.installMemberOrderListKey());
            Map<Integer, List<MallOrderEntity>> memberMap = allOrderList.stream()
                    .collect(Collectors.groupingBy(MallOrderEntity::getMemberId));
            for (Integer memberId : memberMap.keySet()) {
                List<MallOrderEntity> memberOrderList = memberMap.get(memberId);
                List<Integer> memberOrderIds = memberOrderList.stream().map(MallOrderEntity::getId).collect(Collectors.toList());
                stringRedisTemplate.opsForHash().put(RedisCoreKeyUtils.installMemberOrderListKey(), String.valueOf(memberId), JSON.toJSONString(memberOrderIds));
            }

            // 订单详情
            List<Integer> orderIds = allOrderList.stream().map(MallOrderEntity::getId).collect(Collectors.toList());
            List<MallOrderDetailEntity> allOrderDetailList = mallOrderDetailService.list(
                    new QueryWrapper<MallOrderDetailEntity>()
                            .eq("deleted", false)
                            .in("order_id", orderIds)
            );
            Map<Integer, List<MallOrderDetailEntity>> orderDetailMap = allOrderDetailList.stream()
                    .collect(Collectors.groupingBy(MallOrderDetailEntity::getOrderId));

            stringRedisTemplate.delete(RedisCoreKeyUtils.installOrderDetailListKey());
            for (Integer orderId : orderDetailMap.keySet()) {
                List<MallOrderDetailEntity> orderDetailList = orderDetailMap.get(orderId);
                List<MallOrderDeatilRedisDTO> orderDetailRedisList = JSON.parseArray(
                        JSON.toJSONString(orderDetailList), MallOrderDeatilRedisDTO.class
                );

                stringRedisTemplate.opsForHash().put(RedisCoreKeyUtils.installOrderDetailListKey(), String.valueOf(orderId),
                        JSON.toJSONString(orderDetailRedisList));
            }
        }

        return "success";

    }
}
